package cn.hh.pay.service.impl;


import cn.hh.basic.execption.BusinessException;
import cn.hh.basic.service.impl.BasicServiceImpl;
import cn.hh.order.service.IProductOrderService;
import cn.hh.pay.domain.AlipayInfo;
import cn.hh.pay.domain.PayBill;
import cn.hh.pay.mapper.AlipayInfoMapper;
import cn.hh.pay.mapper.PayBillMapper;
import cn.hh.pay.service.IPayBillService;
import cn.hh.pay.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BasicServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IProductOrderService productOrderService;

    @Override
    public String pay(PayBill payBill) {
        // 1.参数校验
        /*1.支付订单信息是否为空，为空抛错*/
        if(payBill==null){
            throw new BusinessException("账单异常，请等待");
        }
        /*2.通过支付订单中的UnionPaySn订单编号查询支付订单，为空抛错*/
        PayBill bill = payBillMapper.loadByUnionPaySn(payBill.getUnionPaySn());
        if(bill==null){
            throw new BusinessException("账单异常，请等待");
        }
        // 2.使用switch循环判断使用什么支付方式
        /*1.通过shop_id商铺id获取AlipayInfo(为了获取appid和公私钥)*/
        switch (payBill.getPayChannel().intValue()){
            /*支付宝*/
            case 1:
                AlipayInfo info = alipayInfoMapper.loadByShopId(payBill.getShop_id());
                System.out.println("======="+AlipayUtils.pay(info, payBill));
                return AlipayUtils.pay(info,payBill );
            case 2 ://2 微信 TODO
                break;
            case 3 ://3 银联 TODO
                break;
            default : //0  余额  TODO
        }
        /*2.调用AlipayUtils支付宝支付工具类，传入AlipayInfo支付宝支付信息和PayBill支付订单信息*/
        return null;
    }

    @Override
    public PayBill loadByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }
}
